package com.yuntongxun.ecdemo.voicechat.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.yuntongxun.ecdemo.voicechat.bean.DBTopic;

import java.util.ArrayList;
import java.util.List;

/** 数据库操作类
 * Created by li_zhuonan on 2016/12/15.
 */
public class DBManager {

    private Context context = null;;
    private static SQLiteDatabase db = null;
    public static final String NAME="voicecha";
    DataBaseHelper dbHelper;

    public DBManager(Context context){
        this.context=context;
        dbHelper = new DataBaseHelper(context, NAME);
    }


    public boolean addTopic(DBTopic topic){
        db=dbHelper.getWritableDatabase();
        try{
            ContentValues values = new ContentValues();
            values.put("cid",topic.getCid());
            values.put("topic_info",topic.getTopicInfo());
            values.put("topic_type",topic.getType());
            db.insert(DataBaseHelper.TOPIC,null,values);
            return true;
        }finally {
            db.close();
        }
    }

    public DBTopic getTopic(String eid){
        DBTopic topic=null;
        db = dbHelper.getWritableDatabase();
        if (db!=null) {
            Cursor cursor=db.query(DataBaseHelper.TOPIC, new String[] { "cid","topic_info","topic_type"}, "cid=?", new String[]{eid}, null, null, null);
            try {
                if (cursor.moveToFirst()&&cursor!=null) {
                    topic = new DBTopic();
                    topic.setCid(cursor.getString(cursor.getColumnIndex("cid")));
                    topic.setTopicInfo(cursor.getString(cursor.getColumnIndex("topic_info")));
                    topic.setType(cursor.getInt(cursor.getColumnIndex("topic_type")));
                }
            } finally {
                cursor.close();
                db.close();
            }

        }
        return topic;
    }



    public List<DBTopic> getAllTopic(){
        List<DBTopic> list=null;
        db = dbHelper.getWritableDatabase();
        if (db != null) {
            Cursor cursor = db.query(DataBaseHelper.TOPIC, new String[] { "cid","topic_info","topic_type"}, null, null, null, null, "id  asc");
            list = new ArrayList<DBTopic>();
            try{
                while(cursor.moveToNext()){
                    DBTopic topic = new DBTopic();
                    topic.setCid(cursor.getString(cursor.getColumnIndex("cid")));
                    topic.setTopicInfo(cursor.getString(cursor.getColumnIndex("topic_info")));
                    topic.setType(cursor.getInt(cursor.getColumnIndex("topic_type")));
                    list.add(topic);
                }
            }finally{
                cursor.close();
                db.close();
            }

        }
        return list;
    }

    public void deleteTopic(String id) {
        db = dbHelper.getWritableDatabase();
        if (db != null) {
            db.delete(DataBaseHelper.TOPIC, "cid=? ",
                    new String[] { id });
            db.close();
        }
    }

}
